﻿﻿
var DoScr;
var Marquee = {
    init: function(id, toFollow, speed) {
        this.speed = speed || 10;
        this.boxID = id;
        this.toFollow = toFollow;
        this.scrollBox = document.getElementById(id);


        if (this.toFollow == "top" || this.toFollow == "bottom") {
            this.appendBox = first(this.scrollBox).cloneNode(true);
            this.scrollBox.appendChild(this.appendBox);
        }
        else {
            var templateLeft = "<table cellspacing='0′ cellpadding='0′ style='border-collapse:collapse;display:inline;'><tr><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>" + this.scrollBox.innerHTML + "</td><td noWrap=true style='white-space: nowrap;word-break:keep-all;'>" + this.scrollBox.innerHTML + "</td></tr></table>";
            this.scrollBox.innerHTML = templateLeft;
            this.appendBox = first(first(first(first(this.scrollBox))));

        }
        this.objs = {
            scrollBox: this.scrollBox,
            appendBox: this.appendBox,
            toFollow: this.toFollow,
            speed: this.speed,
            id: this.boxID
        };
        return this;
    },
    stop: function() {
        if (this && this.objs && this.objs.doScr) {
            clearInterval(this.objs.doScr);
            document.getElementById(this.objs.id).innerHTML = document.getElementById(this.objs.id).getElementsByTagName("td")[0].innerHTML;
        }
    },
    scroll: function() {
        var self = this.objs;
        self.begin = function() {

            switch (self['toFollow']) {
                case "top":
                    self.doScr = setInterval(function() {
                        if (self['appendBox'].offsetHeight <= self['scrollBox'].scrollTop) {
                            self['scrollBox'].scrollTop -= first(self['scrollBox']).offsetHeight;
                        }
                        else {
                            self['scrollBox'].scrollTop++;
                        }
                    }, self.speed);
                    break;
                case "bottom":
                    self.doScr = setInterval(function() {
                        if (self['scrollBox'].scrollTop <= 0) {
                            self['scrollBox'].scrollTop = self['appendBox'].offsetHeight;
                        }
                        else {
                            self['scrollBox'].scrollTop--;
                        }
                    }, self.speed);
                    break;
                case "left":
                    self.doScr = setInterval(function() {
                        if (self['appendBox'].offsetWidth <= self['scrollBox'].scrollLeft) {
                            self['scrollBox'].scrollLeft -= self['appendBox'].offsetWidth;
                        }
                        else {
                            self['scrollBox'].scrollLeft++;
                        }
                    }, self.speed);
                    break;
                case "right":
                    self.doScr = setInterval(function() {
                        if (self['scrollBox'].scrollLeft <= 0) {
                            self['scrollBox'].scrollLeft = self['appendBox'].offsetWidth;
                        }
                        else {
                            self['scrollBox'].scrollLeft--;
                        }

                    }, self.speed);
                    break;
            }
        }
        self.begin();
        self.scrollBox.onmouseover = function() {
            clearInterval(self.doScr);
        }

        self.scrollBox.onmouseout = function() {
            self.begin();
        }
    }
}
function next(elem) {
    do {
        elem = elem.nextSibling;
    } while (elem && elem.nodeType != 1);
    return elem;
}
//—查找第一个子元素的函数—//        
function first(elem) {
    elem = elem.firstChild;
    return elem && elem.nodeType != 1 ? next(elem) : elem;
} 